//
//  PictureView.m
//  UI06_UIScrollView_UIPageControl
//
//  Created by Bodi on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PictureView.h"
#import "RainScrollView.h"
@interface PictureView()<UIScrollViewDelegate>
@property (nonatomic, retain) RainScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;

@end

@implementation PictureView
- (void)dealloc{
    [_imageArray release];
    [_scrollView release];
    [_pageControl release];
    [super dealloc];

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        /* 在初始化的时候对所有属性进行初始化 */
        self.imageArray = [NSArray array];
        
        [self createSubView];
    }
    return self;
}
/** 创建子视图 */
- (void)createSubView{
    /** 创建scrollView, 并添加到父视图上 */
   self.scrollView = [[RainScrollView alloc]initWithFrame:self.bounds];
    self.scrollView.backgroundColor  = [UIColor whiteColor];
    /*设置偏移量*/
    self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
    [self addSubview:_scrollView];
    [_scrollView release];
    /** 指定代理人 */
    self.scrollView.delegate = self;
   
    
    
    
    /** 创建pageControl, 并添加到父视图上 */
    
    
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) / 5 * 3, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) / 5 * 1)];
    [self addSubview:self.pageControl];
    self.pageControl.numberOfPages = _imageArray.count  ;
    [self.pageControl release];
    [self.pageControl addTarget:self action:@selector(pathAction:) forControlEvents:UIControlEventValueChanged];

}
/* pagecomtrol的方法*/
- (void)pathAction:(UIPageControl *)pageControl{
    /*
     * 重点: scrollView 的偏移量
     * x代表的是水平方向的偏移量, y代表的是垂直方向的偏移量, 他们都是相对滚动范围的原点来说的
     */
    
//    self.scrollView.contentOffset = CGPointMake((pageControl.currentPage + 1) *  CGRectGetWidth(self.bounds), 0);
    [self.scrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) *  CGRectGetWidth(self.bounds), 0)];

}

/* UIScrollView的代理方法 */

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    /**当偏移量 大于等于 最后一张图片的位置时, 将scrollView的偏移量改为实际展示的第一张图片的位置*/
    if (self.scrollView.contentOffset.x >= (self.imageArray.count - 1) * self.bounds.size.width) {
        self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds), 0);
        
    }else if (self.scrollView.contentOffset.x <= 0) {
        
        self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.bounds) * (self.imageArray.count - 2), 0);
    }
    
   self.pageControl.currentPage = scrollView.contentOffset.x / CGRectGetWidth(self.bounds ) - 1;
}
///* 放大缩小 */
//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
//    return scrollView.subviews.firstObject;
//
//}




// 重写setter方法


- (void)setImageArray:(NSArray<UIImage *> *)imageArray{
    if (_imageArray != imageArray) {
        [_imageArray release];
        _imageArray = [imageArray retain];
    }
    /*添加图片*/
    [self.scrollView addPictures:_imageArray];
    self.pageControl.numberOfPages = _imageArray.count - 2;


}

@end
